package controllers

import (
	"demo/models"
	"encoding/json"
	"github.com/beego/beego/v2/core/config"
	beego "github.com/beego/beego/v2/server/web"
)

type UserController struct {
	beego.Controller
}

func (c *UserController) GetUserName() {
	var (
	//id    int
	//err   error
	//title string
	)

	var (
		title string
	)

	id, err := c.GetInt("id")
	user, err := models.UserInfo(id)
	if err != nil {
		title = "user not exist"
	} else {
		title = "username: " + user.Username
	}

	c.Ctx.WriteString(title)

	//o := orm.NewOrm()
	//user := new(models.Users)
	//user.Username = "slene"
	//fmt.Println(o.Insert(user))
}

func (c *UserController) List() {

	val, _ := config.String("dev::defaultdb")
	c.Ctx.WriteString(val)
	//c.Ctx.WriteString("api/user/List")
}

func (c *UserController) GetList() {
	c.Ctx.WriteString("api/user/GetList")
}

func (c *UserController) PostList() {
	c.Ctx.WriteString("api/user/PostList")
}

// @router /user/list/:id([0-9]+) [get]
func (u *UserController) Test() {
	u.Ctx.WriteString("UserController@Test func\n")
	id := u.Ctx.Input.Param(":id")
	u.Ctx.WriteString(id)
}

// GetParams
// Desc:
// Users: zouqihui
// Date: 2022-11-22 17:41:38
// @receiver c
// http://127.0.0.1:8080/params?test=1111&a=111
func (c *UserController) GetParams() {
	val := c.GetString("test", "default_val")
	c.Ctx.WriteString(val)

	//打印所有参数
	input, err := c.Input()
	if err != nil {
		return
	}
	json, err := json.Marshal(input)
	if err != nil {
		return
	}
	c.Ctx.WriteString(string(json))
}
